package com.epfresh.adapter;

import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.universalimageloader.core.imageaware.ImageViewAware;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.bean.CommentLoad;

import java.util.ArrayList;
import java.util.List;
import com.epfresh.api.widget.PhotoUploadView;

/**
 * Created by wzh on 2016/5/6.
 */
public class CommentGoodsAdapter extends BaseAdapter {

    public String storeName;

    public Context context;
    public List<CommentLoad.StoreListBean.StoreItemListBean> list;
    public int nowPosition;
    boolean firstTouch;

    public CommentGoodsAdapter(Context context, ArrayList<CommentLoad.StoreListBean.StoreItemListBean> list) {
        this.context = context;
        this.list = list;
    }

    private View.OnClickListener onItemClickListener;

    public View.OnClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(View.OnClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            vh = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_comment_store, null, false);
            vh.tv_comment_store_name = (TextView) convertView.findViewById(R.id.tv_comment_store_name);
            vh.tv_comment_goods = (TextView) convertView.findViewById(R.id.tv_comment_goods);
            vh.iv_common_product = (ImageView) convertView.findViewById(R.id.iv_common_product);
            vh.v_top_1 = (TextView) convertView.findViewById(R.id.v_top_1);
            vh.v_top_2 = (TextView) convertView.findViewById(R.id.v_top_2);
            vh.v_top_3 = (TextView) convertView.findViewById(R.id.v_top_3);
            vh.tv_add_1 = (TextView) convertView.findViewById(R.id.tv_add_1);
            vh.tv_add_2 = (TextView) convertView.findViewById(R.id.tv_add_2);
            vh.tv_add_3 = (TextView) convertView.findViewById(R.id.tv_add_3);
            vh.pu_cover_1 = (PhotoUploadView) convertView.findViewById(R.id.pu_cover_1);
            vh.pu_cover_1.setOnClickListener(onItemClickListener);
            vh.pu_cover_2 = (PhotoUploadView) convertView.findViewById(R.id.pu_cover_2);
            vh.pu_cover_2.setOnClickListener(onItemClickListener);
            vh.pu_cover_3 = (PhotoUploadView) convertView.findViewById(R.id.pu_cover_3);
            vh.pu_cover_3.setOnClickListener(onItemClickListener);

            vh.v_top_1.setOnClickListener(onItemClickListener);
            vh.v_top_2.setOnClickListener(onItemClickListener);
            vh.v_top_3.setOnClickListener(onItemClickListener);

            vh.et_comment_content = convertView.findViewById(R.id.et_comment_content);

            vh.et_comment_content.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent event) {
                    //注意，此处必须使用getTag的方式，不能将position定义为final，写成mTouchItemPosition = position
                    nowPosition = (Integer) view.getTag();
                    firstTouch = true;
                    return false;
                }
            });
            vh.watcher = new EtTextChanged(position);
            vh.et_comment_content.addTextChangedListener(vh.watcher);

            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        CommentLoad.StoreListBean.StoreItemListBean bean = list.get(position);
        vh.tv_comment_store_name.setText(storeName);
        vh.tv_comment_goods.setText(bean.getProductTitle());
        vh.watcher.setPosition(position);
        vh.et_comment_content.setText(bean.getContent());


        ImageLoader.getInstance().displayImage(bean.getProductImgUrl(), new ImageViewAware(vh.iv_common_product));
        initCommentImages(vh,bean);
        vh.pu_cover_1.setTag(R.id.item_key_position,position);
        vh.pu_cover_1.setTag(R.id.item_key_status,bean);
        vh.pu_cover_2.setTag(R.id.item_key_position,position);
        vh.pu_cover_2.setTag(R.id.item_key_status,bean);
        vh.pu_cover_3.setTag(R.id.item_key_position,position);
        vh.pu_cover_3.setTag(R.id.item_key_status,bean);

        vh.v_top_1.setTag(R.id.item_key_position,position);
        vh.v_top_2.setTag(R.id.item_key_position,position);
        vh.v_top_3.setTag(R.id.item_key_position,position);


        vh.et_comment_content.setTag(position);

        if (nowPosition == position && firstTouch) {
            vh.et_comment_content.requestFocus();
            vh.et_comment_content.setSelection(vh.et_comment_content.getText().length());
        } else {
            vh.et_comment_content.clearFocus();
        }


        return convertView;
    }

    public void initCommentImages(ViewHolder vh, CommentLoad.StoreListBean.StoreItemListBean bean){

/*
        ViewGroup.LayoutParams params =  vh.pu_cover_1.getLayoutParams();

        params.height = (LocalDisplay.SCREEN_WIDTH_PIXELS - LocalDisplay.dp2px(80))/3;
        params.width = params.height;
        Log.e("height",""+params.height);
        Log.e("width",""+params.width);
        vh.pu_cover_1.setLayoutParams(params);
        vh.pu_cover_2.setLayoutParams(params);
        vh.pu_cover_3.setLayoutParams(params);
*/

        vh.pu_cover_1.setVisibility(View.INVISIBLE);
        vh.pu_cover_2.setVisibility(View.INVISIBLE);
        vh.pu_cover_3.setVisibility(View.INVISIBLE);
        vh.v_top_1.setVisibility(View.INVISIBLE);
        vh.v_top_2.setVisibility(View.INVISIBLE);
        vh.v_top_3.setVisibility(View.INVISIBLE);
        if(bean.getImgList()==null||bean.getImgList().size()==0){
            vh.pu_cover_1.setVisibility(View.VISIBLE);
            vh.pu_cover_1.getImagView().setImageBitmap(null);
        }

        if(bean.getImgList()!=null&&bean.getImgList().size()>0) {
            vh.pu_cover_1.setVisibility(View.VISIBLE);
            ImageLoader.getInstance().displayImage(bean.getImgList().get(0).getFilePath(), new ImageViewAware(vh.pu_cover_1.getImagView()));
            vh.v_top_1.setVisibility(View.VISIBLE);
            vh.v_top_1.bringToFront();
            vh.pu_cover_2.setVisibility(View.VISIBLE);
            vh.pu_cover_2.getImagView().setImageBitmap(null);
        }
        if(bean.getImgList()!=null&&bean.getImgList().size()>1) {
            vh.pu_cover_2.setVisibility(View.VISIBLE);
            ImageLoader.getInstance().displayImage(bean.getImgList().get(1).getFilePath(), new ImageViewAware(vh.pu_cover_2.getImagView()));
            vh.v_top_2.setVisibility(View.VISIBLE);
            vh.v_top_2.bringToFront();
            vh.pu_cover_3.setVisibility(View.VISIBLE);
            vh.pu_cover_3.getImagView().setImageBitmap(null);
        }
        if(bean.getImgList()!=null&&bean.getImgList().size()>2) {
            vh.pu_cover_3.setVisibility(View.VISIBLE);
            ImageLoader.getInstance().displayImage(bean.getImgList().get(2).getFilePath(), new ImageViewAware(vh.pu_cover_3.getImagView()));
            vh.v_top_3.setVisibility(View.VISIBLE);
            vh.v_top_3.bringToFront();
        }
    }


    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }


    public class ViewHolder {
        TextView tv_comment_store_name;
        TextView tv_comment_goods;
        ImageView iv_common_product;
        EditText et_comment_content;
        EtTextChanged watcher;

        PhotoUploadView pu_cover_1;
        PhotoUploadView pu_cover_2;
        PhotoUploadView pu_cover_3;

        TextView tv_add_1;
        TextView tv_add_2;
        TextView tv_add_3;

        View v_top_1;
        View v_top_2;
        View v_top_3;
    }
/*

    @Override
    publci
*/

/*
    class FocusChange implements View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                EditText etxt = (EditText) v;
                holder.editText.setText(etxt.getText().toString());
            }
        }
    }
*/


    class EtTextChanged implements TextWatcher {
        private int position;

        public EtTextChanged(int position){
            this.position = position;
        }


        public void setPosition(int position) {
            this.position = position;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            if(TextUtils.isEmpty(s)) {
                writeContent.write(position,"");
                return;
            }
            writeContent.write(position,s.toString().trim());
        }
    }
    public interface WriteContent{
        void write(int position, String content);
    }
    private WriteContent writeContent;

    public WriteContent getWriteContent() {
        return writeContent;
    }

    public void setWriteContent(WriteContent writeContent) {
        this.writeContent = writeContent;
    }
}
